package cyningxu.findviewbyx.action;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.EverythingGlobalScope;
import com.intellij.psi.search.FilenameIndex;
import cyningxu.findviewbyx.util.Utils;

import java.io.File;
import java.util.ArrayList;

public class FindViewByXAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent anActionEvent) {
        PsiFile psiFile = anActionEvent.getData(LangDataKeys.PSI_FILE);
        Project project = anActionEvent.getProject();
        if (psiFile == null || project == null) {
            return;
        }
        ArrayList<PsiFile> psiFiles = new ArrayList<>();
        String layoutPath = "";
        if (psiFile.getName().endsWith(".xml")) {
            psiFiles.add(psiFile);
            layoutPath = psiFile.getContainingDirectory().toString().replace("PsiDirectory:", "");
        } else if (psiFile.getName().endsWith(".java")) {
            String javaPath = psiFile.getContainingDirectory().toString().replace("PsiDirectory:", "");
            String javaPathKey = "src" + File.separator + "main" + File.separator + "java";
            int indexOf = javaPath.indexOf(javaPathKey);
            if (indexOf != -1) {
                layoutPath = javaPath.substring(0, indexOf) + "src" + File.separator + "main" + File.separator + "res" + File.separator + "layout";
            }
            String content = psiFile.getText();
            ArrayList<Integer> integerArrayList = searchKeyWordsIndex(content, "R.layout");
            for (Integer index : integerArrayList) {
                int k1 = content.indexOf(")", index);
                int k2 = content.indexOf(",", index);
                if (k1 == -1 && k2 == -1) {
                    continue;
                }
                int k;
                if (k1 == -1) {
                    k = k2;
                } else if (k2 == -1) {
                    k = k1;
                } else {
                    if (k1 > k2) {
                        k = k2;
                    } else {
                        k = k1;
                    }
                }
                String name = content.substring(index, k).replace("R.layout.", "")
                        .replace("\r\n", "").replace("\n", "").replace("\r", "").replace(" ", "") + ".xml";
                PsiFile[] files = FilenameIndex.getFilesByName(project, name, new EverythingGlobalScope(project));
                if (files != null && files.length != 0 && files[0] != null) {
                    psiFiles.add(files[0]);
                }
            }
        } else {
            Utils.showNotification(project, MessageType.ERROR, "only support java and xml files");
            return;
        }
        if (psiFiles.size() == 0) {
            Utils.showNotification(project, MessageType.ERROR, "sorry,can not find xml-file!");
            return;
        }
        FindViewDialog findViewDialog = new FindViewDialog(project, layoutPath, psiFiles);
        findViewDialog.setTitle("FindViewByX");
        findViewDialog.pack();
        findViewDialog.setLocationRelativeTo(WindowManager.getInstance().getFrame(project));
        findViewDialog.setVisible(true);
    }

    public static ArrayList<Integer> searchKeyWordsIndex(String str, String key) {
        int index = 0;
        ArrayList<Integer> integerArrayList = new ArrayList<>();
        while ((index = str.indexOf(key, index)) != -1) {
            integerArrayList.add(index);
            index = index + key.length();
        }
        return integerArrayList;
    }
}
